//package com.example.pyphone3;
//
//import android.content.Context;
//import android.text.TextUtils;
//import android.util.Log;
//import android.widget.Toast;
//
//import com.alibaba.sdk.android.oss.ClientConfiguration;
//import com.alibaba.sdk.android.oss.OSS;
//import com.alibaba.sdk.android.oss.OSSClient;
//import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
//import com.alibaba.sdk.android.oss.callback.OSSProgressCallback;
//import com.alibaba.sdk.android.oss.common.OSSLog;
//import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
//import com.alibaba.sdk.android.oss.common.auth.OSSStsTokenCredentialProvider;
//import com.alibaba.sdk.android.oss.model.OSSRequest;
//import com.aliyun.oss.ClientException;
//import com.aliyun.oss.ServiceException;
//import com.aliyun.oss.model.ObjectMetadata;
//import com.aliyun.oss.model.PutObjectRequest;
//import com.aliyun.oss.model.PutObjectResult;
//
//import org.apache.commons.fileupload.FileItem;
//import org.apache.commons.fileupload.FileItemFactory;
//import org.apache.commons.fileupload.disk.DiskFileItemFactory;
//import org.apache.commons.io.IOUtils;
//import org.apache.http.HttpResponse;
//import org.apache.http.client.methods.HttpPost;
//import org.apache.http.entity.BasicHttpEntity;
//import org.apache.http.impl.client.DefaultHttpClient;
//import org.apache.http.util.EntityUtils;
//import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.mock.web.MockMultipartFile;
//import org.springframework.web.multipart.MultipartFile;
//
//import java.io.DataOutputStream;
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.OutputStream;
//import java.io.UnsupportedEncodingException;
//import java.net.HttpURLConnection;
//import java.net.URL;
//import java.net.URLEncoder;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.concurrent.TimeUnit;
//import android.content.ContextWrapper;
//import okhttp3.Call;
//import okhttp3.Callback;
//import okhttp3.MediaType;
//import okhttp3.MultipartBody;
//import okhttp3.OkHttpClient;
//import okhttp3.Request;
//import okhttp3.RequestBody;
//import okhttp3.Response;
//
//@ConfigurationProperties(prefix = "spring.aliyunOSS")
//public class AliYunOssUploadOrDownFileConfig{
//    private static final String TAG = AliYunOssUploadOrDownFileConfig.class.getSimpleName();
//    private static AliYunOssUploadOrDownFileConfig aliyunOssUploadFileUtil;
//    /**
//     * 服务器的地址
//     */
//    private static final String ENDPOINT =  "http://oss-cn-beijing.aliyuncs.com";
//
//    private ClientConfiguration conf = null;
//    private OSS oss = null;
//    private PutObjectRequest put = null;
//    private OSSAsyncTask task = null;
//    private OnUploadFile onUploadFile;
//
//    public static final String CALLBACK_URL = "callbackUrl";
//    public static final String CALLBACK_BODY = "callbackBody";
//
//    /**
//     * callback 的回调地址 eg : "http://oss-demo.aliyuncs.com:23450";
//     * 通过这个地址oss可以将所需要的数据传到应用服务器，服务器就可以根据上传的参数为所欲为了
//     */
//    private String mCallbackAddress = "http://oss-demo.aliyuncs.com:23450";
//    private Context mContext;
//    private AliYunOssUploadOrDownFileConfig(Context context){
//        this.mContext = context;
//    }
//
//    public static AliYunOssUploadOrDownFileConfig getInstance(Context context) {
//        if(aliyunOssUploadFileUtil == null) {
//            synchronized (AliYunOssUploadOrDownFileConfig.class) {
//                if(aliyunOssUploadFileUtil == null) {
//                    aliyunOssUploadFileUtil = new AliYunOssUploadOrDownFileConfig(context);
//                }
//            }
//        }
//        return aliyunOssUploadFileUtil;
//    }
//
//
//    /**
//     * 调用这个方法之前必须先设置accessKeyId，accessKeySecret，securityToken;
//     */
//    public  void initOss( String accessKeyId,
//                          String accessKeySecret,
//                          String securityToken){
//        conf = new ClientConfiguration();
//        conf.setConnectionTimeout(5*60*1000);
//        conf.setSocketTimeout(5*60*1000);
//        conf.setMaxConcurrentRequest(5);
//        conf.setMaxErrorRetry(2);
//        OSSLog.enableLog();
//        //请求后台服务器返回
//        //    "StatusCode":200,
//        //    "AccessKeyId":"STS.3p***dgagdasdg",
//        //    "AccessKeySecret":"rpnwO9***tGdrddgsR2YrTtI",
//        //   "SecurityToken":"CAES+wMIARKAAZhjH0EUOIhJMQBMjRywXq7MQ/cjLYg80Aho1ek0Jm63XMhr9Oc5s˙∂˙∂3qaPer8p1YaX1NTDiCFZWFkvlHf1pQhuxfKBc+mRR9KAbHUefqH+rdjZqjTF7p2m1wJXP8S6k+G2MpHrUe6TYBkJ43GhhTVFMuM3BZajY3VjZWOXBIODRIR1FKZjIiEjMzMzE0MjY0NzM5MTE4NjkxMSoLY2xpZGSSDgSDGAGESGTETqOio6c2RrLWRlbW8vKgoUYWNzOm9zczoqOio6c2RrLWRlbW9KEDExNDg5MzAxMDcyNDY4MThSBTI2ODQyWg9Bc3N1bWVkUm9sZVVzZXJgAGoSMzMzMTQyNjQ3MzkxMTg2OTExcglzZGstZGVtbzI=",
//        //   "Expiration":"2017-12-12T07:49:09Z",
//        OSSCredentialProvider credentialProvider = new OSSStsTokenCredentialProvider(
//                accessKeyId, accessKeySecret,
//                securityToken
//        );
//
//        oss = new OSSClient(mContext,ENDPOINT,credentialProvider,conf);
//
//    }
//
//
//    /**
//     * 上传文件
//     * @param bucketName oss上所建的仓库名称  eg : smartDev 如果设置的合理，将会报错，oss会提示说设置的bucketName不可用
//     * @param objectKey oss上所存储文件的名称 eg: vic/test/xxx.mp4
//     * @param uploadFilePath 本地存储文件的路径
//     */
//    public void uploadFile(String bucketName, String objectKey,
//                           String uploadFilePath, final String filename, final int reminderId){
//        put = new PutObjectRequest(bucketName,objectKey, new File(uploadFilePath));
//
//        ObjectMetadata metadata = new ObjectMetadata();
//        metadata.setContentType("application/octet-stream");
//        put.setMetadata(metadata);
////        put.setCRC64(OSSRequest.CRC64Config.YES);
//        if(!TextUtils.isEmpty(mCallbackAddress)) {
////            put.setCallbackParam(new HashMap<String, String>()
//            {
//                {
//                    //这里 filename 中的object 不用替换，如果到oss了，object 将自动在服务器自动被上传文件名替换 并且是全路径名称
////                    put(CALLBACK_URL,mCallbackAddress);put(CALLBACK_BODY,"filename=${object}"+ "&reminder_id=${x:" + reminderId + "}");
////                    put(CALLBACK_BODY,"filename=${object}");
//                }
////            });
//        }
//

//try {
////                zipFile.ZipFolder("/storage/emulated/0/Pictures/[2020-09-16]","/storage/emulated/0/Pictures/202009160.zip");
//        } catch (Exception e) {
//        e.printStackTrace();
//        }
////        put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
////            @Override
////            public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {
////                Log.d(TAG, "currentSize: " + currentSize + " totalSize: " + totalSize);
////            }
////        });
//
//        task = oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
//            @Override
//            public void onSuccess(PutObjectRequest request, PutObjectResult result) {
//                Log.d(TAG,"uploadSuccess");
//                if(onUploadFile != null) {
//                    onUploadFile.onUploadFileSuccess("上传成功");
//                }
//            }
//
//            @Override
//            public void onFailure(PutObjectRequest request, ClientException clientException, ServiceException serviceException) {
//                if(onUploadFile != null) {
//                    try{
//                        if(serviceException != null) {
//                            onUploadFile.onUploadFileFailed(serviceException.toString());
//                        } else {
//                            onUploadFile.onUploadFileFailed("上传失败");
//                        }
//                    }catch (Exception e) {
//                        onUploadFile.onUploadFileFailed("上传失败");
//                    }
//
//                }
//                // Request exception
//                if (clientException != null) {
//                    // Local exception, such as a network exception
//                    clientException.printStackTrace();
//                }
//                if (serviceException != null) {
//                    // Service exception
//                    Log.d(TAG,"ErrorCode="+ serviceException.getErrorCode());
//                    Log.d(TAG,"RequestId=" + serviceException.getRequestId());
//                    Log.d(TAG,"HostId=" + serviceException.getHostId());
//                    Log.d(TAG,"RawMessage=" + serviceException.getRawMessage());
//                }
//            }
//            // task.cancel(); // Cancel the task
//
//            // task.waitUntilFinished(); // Wait till the task is finished
//        });
//    }
//
//    public void setOnUploadFile(OnUploadFile onUploadFile) {
//        this.onUploadFile = onUploadFile;
//    }
//
//    public interface OnUploadFile {
//        void onUploadFileSuccess(String info);
//        void onUploadFileFailed(String errCode);
//    }
//
//}
